package com.mall.utils;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.WebUtils;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @Package: com.alipay.utils
 * @author: xiao-chen
 * @Create: 2023/4/5-16:21
 * @Version: v 1.0
 * @Description:
 */
@Component
@Slf4j
public class AlipayUtil {
    @Value("${alipay.url}")
    private String url;
    @Value("${alipay.appId}")
    private String appId;
    @Value("${alipay.privateKey}")
    private String privateKey;
    @Value("${alipay.publicKey}")
    private String publicKey;
    @Value("${alipay.notifyUrl}")
    private String notifyUrl;
    @Value("${alipay.returnUrl}")
    private String returnUrl;

    /**
     * @Description: 调用支付宝接口进行支付
     * @param: [java.lang.String, float, java.lang.String]
     * @return: java.lang.String
     * @author: xiao-chen
     * @date: 2023/4/5 17:23
     */

    public String pay(String orderId, Double orderPrice, String orderTitle) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient(url, appId, privateKey, "json", "utf-8", publicKey, "RSA2");
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        //关闭 SSL 服务端证书校验
        WebUtils.setNeedCheckServerTrusted(false);
        request.setNotifyUrl(notifyUrl);
        request.setReturnUrl(returnUrl);
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", orderId);
        bizContent.put("total_amount", orderPrice);
        bizContent.put("subject", orderTitle);
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");
        request.setBizContent(bizContent.toString());
        AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
        if (response.isSuccess()) {
            log.error("调用成功");
        } else {
            log.error("调用失败");
        }
        return response.getBody();
    }

    /**
     * @Description: 利用订单ID判断支付是否成功
     * @param: [java.lang.String]
     * @return: boolean
     * @author: xiao-chen
     * @date: 2023/4/5 17:28
     */

    public boolean ckeckPay(String orderId) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient(url, appId, privateKey, "json", "utf-8", publicKey, "RSA2");
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        //关闭 SSL 服务端证书校验
        WebUtils.setNeedCheckServerTrusted(false);
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", orderId);
        request.setBizContent(bizContent.toString());
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        return response.isSuccess();
    }
}
